const { invoke } = window.__TAURI__.core;
import { listen } from '@tauri-apps/api/event';

let username;
let pwd;
let vaildMsgEl;

async function greet() {
  // Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
  greetMsgEl.textContent = await invoke("greet", { name: greetInputEl.value });
}

async function vaild(){
  let res = await invoke("vaild", { name: username.value ,pwd: pwd.value });
  if (res){
    vaildMsgEl.innerHTML = '<font color="green">登录成功</font>';
  }
  else{
    vaildMsgEl.innerHTML = "<font color='red'>登录失败</font>";
  }
}

// window.addEventListener("DOMContentLoaded", () => {
//   username = document.querySelector("#vaild-username");
//   pwd = document.querySelector("#vaild-pwd");
//   vaildMsgEl = document.querySelector("#vaild-msg");
//   document.querySelector("#vaild-form").addEventListener("submit", (e) => {
//     e.preventDefault();
//     vaild();
//   });
// });

async function get_user_info(){
  let res = await invoke("get_user_info", { username: username.value});
  console.log(res);
}

async function set_user_info(){
  const Blood_Type = {
    A:"A",
    B:"B",
    O:"O",
    AB:"AB",
    Rh:"Rh"
  }
  const user_info = {
    username: "godxia",
    nickname: "虾神",
    email:"godxia@abcd.com",
    age: 36,
    birthday: "1988-01-01",
    blood_type:Blood_Type.A,
    other:[1.1,2.2,3,3],
  }

  await invoke("set_user_info", { user: user_info});
}

async function show_proc(){
  invoke('send_message');
  const start_listen = async () => {
      return await listen<Payload>('proc_event', (event) => {
          const { Proc_progress, timestamp } = event.payload;
          console.log("message:", Proc_progress,
              "timestamp:", timestamp);
      });
  };
}

window.addEventListener("DOMContentLoaded", () => {
  username = document.querySelector("#query-username");
  vaildMsgEl = document.querySelector("#query-msg");
  document.querySelector("#query-form").addEventListener("submit", (e) => {
    e.preventDefault();
    get_user_info();
  });
});